package com.jingyong.infosite.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import com.mongodb.BasicDBObject;
import com.mongodb.Block;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;

@Service
public class DealsService {
	@Autowired
	private MongoTemplate mongoTemplate;

	public Object query(String month) {
		MongoCollection<Document> col = mongoTemplate.getCollection("deals");
		BasicDBObject filter = new BasicDBObject();
		filter.append("data.years", month);
		FindIterable<Document> iter = col.find(filter);
		final List<Document> list = new ArrayList<Document>();
		iter.batchSize(10).forEach(new Block<Document>() {
			public void apply(Document _doc) {
				list.addAll((Collection<? extends Document>) _doc.get("data", Document.class)
						.get("platform_data", List.class).stream()/*.skip(15).limit(30)*/.collect(Collectors.toList()));
			}
		});
		return list;
	}
}
